# coding: utf-8
import pandas as pd
import jieba


class EmotionAnalysisDLUT:
    # -------------------------------------情感词典读取-------------------------------
    # 注意：
    # 1.词典中怒的标记(NA)识别不出被当作空值,情感分类列中的NA都给替换成NAU
    # 2.大连理工词典中有情感分类的辅助标注(有NA),故把情感分类列改好再替换原词典中

    # 扩展前的词典
    df = pd.read_excel("./model/emotionAnalysisDir_DLUT/大连理工大学中文情感词汇本体NAU.xlsx", engine='openpyxl')
    df = df[['词语', '词性种类', '词义数', '词义序号', '情感分类', '强度', '极性']]

    # -------------------------------------七种情绪的运用-------------------------------
    Happy = []
    Good = []
    Surprise = []
    Anger = []
    Sad = []
    Fear = []
    Disgust = []
    NetViolence = []

    # df.iterrows()功能是迭代遍历每一行
    for idx, row in df.iterrows():
        if row['情感分类'] in ['PA', 'PE']:
            Happy.append(row['词语'])
        if row['情感分类'] in ['PD', 'PH', 'PG', 'PB', 'PK']:
            Good.append(row['词语'])
        if row['情感分类'] in ['PC']:
            Surprise.append(row['词语'])
        if row['情感分类'] in ['NB', 'NJ', 'NH', 'PF']:
            Sad.append(row['词语'])
        if row['情感分类'] in ['NI', 'NC', 'NG']:
            Fear.append(row['词语'])
        if row['情感分类'] in ['NE', 'ND', 'NN', 'NK', 'NL']:
            Disgust.append(row['词语'])
        if row['情感分类'] in ['NAU']:
            Anger.append(row['词语'])
            # NJ失望 NA愤怒 NE烦闷 ND憎恶 NN贬责 NK妒忌 NA愤怒
        if row['情感分类'] in ['NJ', 'NA', 'NE', 'ND', 'NN', 'NK', 'NA']:
            NetViolence.append(row['词语'])

        # 正负计算不是很准 自己可以制定规则
    Positive = Happy + Good + Surprise
    Negative = Anger + Sad + Fear + Disgust
    print('情绪词语列表整理完成')

    # ---------------------------------------中文分词---------------------------------

    # # 添加自定义词典和停用词
    # stop_list = pd.read_csv('stop_words.txt',
    #                         engine='python',
    #                         encoding='utf-8',
    #                         delimiter="\n",
    #                         names=['t'])
    #
    # # 获取重命名t列的值
    # stop_list = stop_list['t'].tolist()
    # 加载停用词
    with open(r"./static/stopwords/stopwords.txt", 'r', encoding='utf-8') as file:
        stop_list = [line.strip() for line in file]

    # # 读取文本文件，并将每行文本作为一个条目
    # stop_list_df = pd.read_csv('stop_words.txt', header=None, encoding='utf-8')
    # # 将DataFrame转换为列表
    # stop_list = stop_list_df[0].tolist()
    # stop_list = pd.read_csv('./stop_words.txt', engine='python', encoding='utf-8', names=['t'])
    # stop_list = stop_list['t'].tolist()

    @staticmethod
    def classify_text(text):
        positive = 0
        negative = 0
        wordlist = [w for w in jieba.lcut(text) if w not in EmotionAnalysisDLUT.stop_list]
        wordset = set(wordlist)
        for word in wordset:
            freq = wordlist.count(word)
            if word in EmotionAnalysisDLUT.Positive:
                positive += freq
            if word in EmotionAnalysisDLUT.Negative:
                negative += freq
        # 消极返回1，积极返回0
        print(f"大连理工情感词典返回积极{positive}消极{negative}")
        if positive >= negative:
            return 0
        else:
            return 1
